﻿using Engine;
using GameEntitySystem;
using TemplatesDatabase;

namespace Game
{
    public class ComponentThirst : Component, IUpdateable
    {
        private float m_water;

        public ComponentPlayer m_componentPlayer;

        public float Water
        {
            get
            {
                return m_water;
            }
            set
            {
                m_water = MathUtils.Saturate(value);
            }
        }

        public ValueBarWidget WaterBarWidget { get; set; }

        public UpdateOrder UpdateOrder => UpdateOrder.Default;

        public void Update(float dt)
        {
            WaterBarWidget.Value = Water;
            Test();
        }

        public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
        {
            m_componentPlayer = base.Entity.FindComponent<ComponentPlayer>(throwOnError: true);
            Water = valuesDictionary.GetValue<float>("Water");
            ContainerWidget guiWidget = m_componentPlayer.GuiWidget;
            WaterBarWidget = guiWidget.Children.Find<ValueBarWidget>("WaterBar");
            WaterBarWidget.Value = Water;
        }

        public override void Save(ValuesDictionary valuesDictionary, EntityToIdMap entityToIdMap)
        {
            valuesDictionary.SetValue("Water", Water);
        }

        public void Test()
        {
            if (m_componentPlayer.ComponentInput.PlayerInput.Move.Length() > 0f)
            {
                Water -= 2.9E-05f;
            }

            if (Water >= 0.2f && Water <= 0.3f)
            {
                m_componentPlayer.ComponentGui.DisplaySmallMessage("你感到很口渴...", Color.White, blinking: false, playNotificationSound: false);
            }

            if (Water <= 0f)
            {
                m_componentPlayer.ComponentHealth.Injure(6.9E-05f, null, ignoreInvulnerability: false, "你被渴死了");
                m_componentPlayer.ComponentGui.DisplaySmallMessage("赶快寻找水源吧...", Color.White, blinking: true, playNotificationSound: false);
            }

            if (m_componentPlayer.ComponentHealth.Health < 0.3f)
            {
                m_componentPlayer.ComponentHealth.Injure(9E-05f, null, ignoreInvulnerability: false, "你因流血不止身亡了");
                m_componentPlayer.ComponentGui.DisplaySmallMessage("你受到了重创，急需得到治疗...", Color.White, blinking: true, playNotificationSound: false);
            }
        }
    }
}